#include "window.hpp"
#include <stdlib.h>
#include <GL/glut.h>

#include "matrix.cpp"

void init(){
	width = 500;
	height = 500;
	fps = 20;
	//initialisation du modèle taureau et affichage
	m = model::importFromFile("../data/taureau.txt");

	//Aggrandissement du modèle pasque bon voila
	double size = 200.;
	vector<double> v = vector<double>(size,size,size);

	std::cout<<"Changement dim"<<std::endl;
	Matrix<double> matrice = Matrix<double>::MatrixScale(v);
	std::cout<<"Changemdsfdsfsent dim"<<std::endl;
	m->applyMatrix(matrice);

	std::cout<<"fin dim"<<std::endl;
	//m->print();
	
	//initialisation d'une arcball
	vector<int> c;
	a = new arcball(c, width/2, m);

	//test de clik point à point
	//a->mousePressed(0,0);
	//a->mouseDragged(0,100);
	//a->mouseReleased(0,100);

	//Etat souris
	mouseLeft = mouseRight = mouseMiddle = false;
}

void destroy(){
	delete a;
	delete m;
}

void launch(int argc, char **argv){

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB);
	//glutInitWindowPosition(100,100);
	glutInitWindowSize(width,height);
	win = glutCreateWindow("Test");
	glClearColor(0.f,0.f,0.f,0.f);

    // parametrage de la fonction d'affichage (obligatoire)
    glutDisplayFunc(draw);
	glutKeyboardFunc(keyboard);
	glutMouseFunc(mouse);
	glutMotionFunc(motion);
	glutIdleFunc(wait);

    // boucle principale de GLUT
    glutMainLoop();
}


void wait(){
	static int nWaitUntil = glutGet(GLUT_ELAPSED_TIME);
	int nTimer = glutGet(GLUT_ELAPSED_TIME);
	if(nTimer >= nWaitUntil){
		glutPostRedisplay();
		nWaitUntil = nTimer + (1000 / fps);
	}
}

void keyboard(unsigned char touche, int x, int y){
	if(touche=='q' || touche == 27){
		glutDestroyWindow(win);
		return;
	}
	if(touche=='z'){	//Diminution de la taille du modèle de 10%
		double size = 0.9;
		vector<double> v = vector<double>(size,size,size);
		Matrix<double> matrice = Matrix<double>::MatrixScale(v);
		m->applyMatrix(matrice);
		return;
	}
	if(touche=='s'){ 	//Augmentation de la taille du modèle de 10%
		double size = 1.1;
		vector<double> v = vector<double>(size,size,size);
		Matrix<double> matrice = Matrix<double>::MatrixScale(v);
		m->applyMatrix(matrice);
		return;
	}

}

void mouse(int button, int state, int x, int y){

	//Changement repère
	x = x - width/2;
	y = height/2 - y;

	switch(button){
		case GLUT_LEFT_BUTTON:
			/*	Transformer pour changer le repere */
			if (state == GLUT_DOWN){
				cout<<"Début clic gauche"<<endl;
				mouseLeft = true;
				a->mousePressed(x,y);
			}
			if (state == GLUT_UP){
				cout<<"Fin clic gauche"<<endl;
				mouseLeft = false;
				a->mouseReleased(x,y);
			}
			break;
		case GLUT_RIGHT_BUTTON:
			if( state == GLUT_DOWN){
				mouseRight = true;
				cout<<"Début clic droit"<<endl;
			}
			if (state == GLUT_UP){
				mouseRight = false;
				cout<<"Fin clic droit"<<endl;
			}
			break;
		case GLUT_MIDDLE_BUTTON:
			if( state == GLUT_DOWN){
				mouseMiddle = true;
				cout<<"Début clic milieu"<<endl;
			}
			if (state == GLUT_UP){
				mouseMiddle = false;
				cout<<"Fin clic milieu"<<endl;
			}
			break;
	}
}

void motion(int x, int y){
	//Changement repère
	x = ( x - width/2) ;
	y = (height/2 - y) ;
	cout<<x<<" "<<y<<endl;

	if(mouseLeft){
		a->mouseDragged(x,y);
	}
}

void draw(){
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f,1.0f,1.0f);		//ROUGE HAHAHAHAHA

	m->draw();

	glFlush();
}
